<?php
namespace app\common\model;
use app\common\model\SubCenterRel as SubCenterRelModel;
use think\Validate;
use think\Db;
use think\facade\Log;

class SubCenterRel extends Common
{
    protected $rule = [
    ];
    protected $msg = [
    ];

    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);

        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;

        return $re;
    }

    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];

        if(isset($post['dep_id']) && $post['dep_id'] != ""){
            $where[] = ['dep_id', 'eq', $post['dep_id']];
        }
        if(isset($post['specialty_id']) && $post['specialty_id'] != ""){
            $where[] = ['specialty_id', 'eq', $post['specialty_id']];
        }
        if(isset($post['subcenter_id']) && $post['subcenter_id'] != ""){
            $where[] = ['subcenter_id', 'eq', $post['subcenter_id']];
        }

        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['dep_id ASC, subcenter_id asc, specialty_id asc'];
        return $result;
    }

    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        return $list;
    }
}
